package com.pdf.pdfserver.service.read.text;

import com.pdf.pdfserver.enums.FileSuffixEnum;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;

/**
 * @Author songfz
 * @Description
 * @Date 2022/1/4
 */
@Service
public class PowerPointReadService implements IfReadService {

    private static Logger LOG = LoggerFactory.getLogger(IfReadService.class);

    @Override
    public String read(FileSuffixEnum suffixEnum, InputStream fis) {

        PowerPointExtractor ex = null;
        XMLSlideShow xmlSlideShow = null;
        try {
            if (FileSuffixEnum.PPT.compareTo(suffixEnum) == 0) {
                ex = new PowerPointExtractor(fis);
                return ex.getText();

            } else if (FileSuffixEnum.PPTX.compareTo(suffixEnum) == 0) {
                StringBuilder sb = new StringBuilder();
                xmlSlideShow = new XMLSlideShow(fis);
                List<XSLFSlide> slides = xmlSlideShow.getSlides();
                for (XSLFSlide slide : slides) {
                    CTSlide rawSlide = slide.getXmlObject();
                    CTGroupShape gs = rawSlide.getCSld().getSpTree();
                    CTShape[] shapes = gs.getSpArray();
                    for (CTShape shape : shapes) {
                        CTTextBody tb = shape.getTxBody();
                        if (null == tb) {
                            continue;
                        }
                        CTTextParagraph[] paras = tb.getPArray();
                        for (CTTextParagraph textParagraph : paras) {
                            CTRegularTextRun[] textRuns = textParagraph.getRArray();
                            for (CTRegularTextRun textRun : textRuns) {
                                sb.append(textRun.getT() + " ");
                            }
                        }
                    }
                }
                return sb.toString();

            }
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        } finally {
            Optional.ofNullable(ex).ifPresent(ex1-> {
                try {
                    ex1.close();
                } catch (IOException e) {
                   LOG.error(e.getMessage(),e);
                }
            });
            Optional.ofNullable(xmlSlideShow).ifPresent(xmlSlideShow1 -> {
                try {
                    xmlSlideShow1.close();
                } catch (IOException e) {
                    LOG.error(e.getMessage(),e);
                }
            });
            Optional.ofNullable(fis).ifPresent(in1-> {
                try {
                    in1.close();
                } catch (IOException e) {
                    LOG.error(e.getMessage(),e);
                }
            });
        }
        return null;
    }
}
